package com.ba360.struts.form;

import com.ba360.portal.database.dbo.User;
import java.io.PrintStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.apache.struts.action.ActionError;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;

public class RegistrationForm extends ActionForm
{
  private String email;
  private String city;
  private String password;
  private String email_confirm;
  private String password_confirm;

  public ActionErrors validate(ActionMapping mapping, HttpServletRequest request)
  {
    ActionErrors errors = null;
    String action = request.getParameter("action");

    errors = new ActionErrors();
    HttpSession session = request.getSession();
    if (action.equals("Save"))
    {
      System.out.println("The bean-email is:" + this.email);
      System.out.println("The bean-confirmemail is:" + this.email_confirm);
      System.out.println("The bean-city is:" + this.city);
      System.out.println("The bean-password is:" + this.password);
      System.out.println("The bean-confirm password is:" + this.password_confirm);
      if ((this.email == null) || (this.email.trim().length() == 0))
      {
        errors.add("Email", new ActionError("error.email.value"));
      }

      if ((this.email != null) && ((
        (this.email.indexOf("@") == -1) || (this.email.indexOf(".") == -1) || (this.email.length() < 8))))
      {
        errors.add("Email", new ActionError("error.invalidemail.value"));
      }

      if ((this.email_confirm == null) || (this.email_confirm.trim().length() == 0))
      {
        errors.add("Confirm Email", new ActionError("error.email.value"));
      }

      if ((this.email_confirm != null) && 
        (!(this.email_confirm.equals(this.email))))
      {
        errors.add("Confirm Email", new ActionError("error.email.value"));
      }

      if ((this.city == null) || (this.city.trim().length() == 0))
      {
        errors.add("City", new ActionError("error.city.value"));
      }

      if ((this.password == null) || (this.password.trim().length() == 0))
      {
        errors.add("Password", new ActionError("error.password.value"));
      }
      if ((this.password != null) && 
        (this.password.trim().length() < 6))
      {
        errors.add("Password", new ActionError("error.password.value"));
      }

      if ((this.password_confirm != null) && 
        (!(this.password_confirm.equals(this.password))))
      {
        errors.add("Confirm Password", new ActionError("error.password.value"));
      }

    }
    else if (action.equals("UpdateInfo"))
    {
      if ((this.email == null) || (this.email.trim().length() == 0))
      {
        errors.add("Email", new ActionError("error.email.value"));
      }

      if ((this.email != null) && ((
        (this.email.indexOf("@") == -1) || (this.email.indexOf(".") == -1) || (this.email.length() < 8))))
      {
        errors.add("Email", new ActionError("error.invalidemail.value"));
      }

      if ((this.city == null) || (this.city.trim().length() == 0))
      {
        errors.add("City", new ActionError("error.city.value"));
      }

      if ((this.password == null) || (this.password.trim().length() == 0))
      {
        errors.add("Password", new ActionError("error.password.value"));
      }
      if ((this.password != null) && 
        (this.password.trim().length() < 6))
      {
        errors.add("Password", new ActionError("error.password.size"));
      }

    }
    else if (action.equals("ViewInfo")) {
      User user = (User)session.getAttribute("USER");
      if (user == null) {
        errors.add("NOT_LOGGED", new ActionError("error.user.nologin"));
        return errors;
      }

      this.email = user.getEmail();
      this.password = user.getPwd();
      this.city = user.getCity();
    }

    return errors;
  }

  public void reset(ActionMapping mapping, HttpServletRequest request)
  {
    this.email = null;
    this.email_confirm = null;
    this.city = null;
    this.password = null;
    this.password_confirm = null;
  }

  public String getPassword()
  {
    return this.password;
  }

  public void setPassword(String password)
  {
    this.password = password;
  }

  public String getEmail()
  {
    return this.email;
  }

  public void setEmail(String email)
  {
    this.email = email;
  }

  public String getCity()
  {
    return this.city;
  }

  public void setCity(String city)
  {
    this.city = city; }

  public String getPassword_confirm() {
    return this.password_confirm;
  }

  public void setPassword_confirm(String password_confirm) {
    this.password_confirm = password_confirm;
  }

  public String getEmail_confirm() {
    return this.email_confirm;
  }

  public void setEmail_confirm(String email_confirm) {
    this.email_confirm = email_confirm;
  }
}